package com.equipment;

import java.util.NoSuchElementException;

import com.util.EncodingUtil;

/**
 * D8 OBU交易应答
 * @author Administrator
 *
 */
public class OBUTransactionResponse {

	// 帧开始标记
	private String stx = "55AA";
	// 串口帧序列号
	private String rsctl = "00";
	// 长度
	private String Length = "";
	// 代码类型
	private String code = "DF";
	// 拓展子码
	private String subCode = "D8";
	// 状态
	private String Status = "";
	// 终端机交易序号
	private String TermTradeNo = "";
	// 用户卡脱机交易序号
	private String CardTradeNo = "";
	// 交易TAC码
	private String TAC = "";
	// 交易前余额
	private String Balance1 = "";
	// 交易后余额
	private String Balance2 = "";
	// 异或校验值
	private String bcc = "ec";
	/**
	 * 解析D8 OBU交易应答
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public OBUTransactionResponse analysis(String context) throws NoSuchElementException{
		OBUTransactionResponse otresponse = null;
		if (!context.substring(12, 14).equals("D7")) {
			throw new NoSuchElementException("OBU交易帧指令错误");
		}
		// 判断是否成功
		if (!context.substring(14, 16).equals("00")) {
			throw new NoSuchElementException("OBU交易失败:"+EncodingUtil.getErrorResult(EncodingUtil.hexToDec(context.substring(14, 16))));
		}
		try {
			otresponse = new OBUTransactionResponse();
			otresponse.Status = context.substring(14, 16);
			otresponse.TermTradeNo = context.substring(16, 24);
			otresponse.CardTradeNo = context.substring(24, 28);
			otresponse.TAC = context.substring(28, 36);
			otresponse.Balance1 = context.substring(36, 44);
			otresponse.Balance2 = context.substring(44,52);
		} catch (Exception e) {
			throw new NoSuchElementException("OBU交易失败");
		}
		return otresponse;
	}
	public String getStx() {
		return stx;
	}
	public void setStx(String stx) {
		this.stx = stx;
	}
	public String getRsctl() {
		return rsctl;
	}
	public void setRsctl(String rsctl) {
		this.rsctl = rsctl;
	}
	public String getLength() {
		return Length;
	}
	public void setLength(String length) {
		Length = length;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getSubCode() {
		return subCode;
	}
	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}
	public String getStatus() {
		return Status;
	}
	public void setStatus(String status) {
		Status = status;
	}
	public String getTermTradeNo() {
		return TermTradeNo;
	}
	public void setTermTradeNo(String termTradeNo) {
		TermTradeNo = termTradeNo;
	}
	public String getCardTradeNo() {
		return CardTradeNo;
	}
	public void setCardTradeNo(String cardTradeNo) {
		CardTradeNo = cardTradeNo;
	}
	public String getTAC() {
		return TAC;
	}
	public void setTAC(String tAC) {
		TAC = tAC;
	}
	public String getBalance1() {
		return Balance1;
	}
	public void setBalance1(String balance1) {
		Balance1 = balance1;
	}
	public String getBalance2() {
		return Balance2;
	}
	public void setBalance2(String balance2) {
		Balance2 = balance2;
	}
	public String getBcc() {
		return bcc;
	}
	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

}
